﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ExaminationTT.Model;
using OutputGenerator;

namespace HTMLOutputGenerator
{
    public class HtmlOutputGenerator : IOutputGenerator
    {
        private string strItable = "<table>";
        private string strFtable = "</table>";
        private string strItrow = "<tr>";
        private string strFtrow = "</tr>";
        private string strItdata = "<td>";
        private string strFtdata = "</td>";

        private Form f;
        private SaveFileDialog sfd;

        private IEnumerable<IExam> _lExam;

        public void Setdata(IEnumerable<IExam> lexam)
        {
            if(lexam == null || lexam.Count() == 0) return;
            _lExam = lexam;
        }
        public void GenerateOutPut()
        {
            f = new Form();
            sfd = new SaveFileDialog();
            sfd.Filter = "HTML File|*.html";
            sfd.Title = "Save Exam Map HTML";
            if(sfd.ShowDialog() == DialogResult.Cancel) return;
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] str;
            System.IO.FileStream fs = (System.IO.FileStream) sfd.OpenFile();
            if (sfd.FileName != "")
            {
                str = encoding.GetBytes(strItable);
                fs.Write(str, 0, str.Length);
                foreach (var exam in _lExam)
                {

                    str = encoding.GetBytes(strItrow);
                    fs.Write(str, 0, str.Length);
                    str = encoding.GetBytes(strItdata + exam.CUnit.Acronym + strFtdata);
                    fs.Write(str, 0, str.Length);
                    str = encoding.GetBytes(strItdata + exam.ExamDate + strFtdata);
                    fs.Write(str, 0, str.Length);
                    foreach (var room in exam.Rooms)
                    {
                        str = encoding.GetBytes(strItdata + room.Room.Description + strFtdata);
                        fs.Write(str, 0, str.Length);  
                    }
                    str = encoding.GetBytes(strFtrow);
                    fs.Write(str, 0, str.Length);
                }
                
                    str = encoding.GetBytes(strFtable);
                    fs.Write(str, 0, str.Length);
                    fs.Close();
                MessageBox.Show("File Saved");
            }
        }

        public string Getname()
        {
            return "HTML Generator";
        }
    }
}
